<?php
namespace app\components\pay;

use Yii;
use yii\base\Exception;

use app\components\KelaileJava;

use app\models\App;
use app\models\Order;
use app\models\OrderPay;
use app\models\PaymentAccount;

//百度钱包支付组件
class BaiduPay extends Pay {
    
    CONST BAIDU_PAY_JAVA_URL = 'superpay/micropay'; //百度被扫支付JAVA 
    
    //被扫支付
    public function micropay(array $params) {
        if(!isset($params['device_no'])) throw new \Exception('device_no为空',2022019);
        if(!isset($params['out_trade_no'])) throw new \Exception('out_trade_no为空',2022015);
        if(!isset($params['total_fee'])) throw new \Exception('total_fee为空',2022016);
        if(!isset($params['auth_code'])) throw new \Exception('auth_code为空',2022020);
        if(!isset($params['body'])) throw new \Exception('body为空',2022017);
        $params['notify_url'] = Func::getHostInfo() . '/' . KelaileJava::NOTIFY_URL;
        $params['account_info'] = [
            KelaileJava::BAIDU_PAY => [
                'account' => $this->paymentObj->partner_id,
            ],
        ];
        $javaRes = self::getResBuyJavaApi(self::BAIDU_PAY_JAVA_URL, $params);
        return $javaRes;
    }
    
    /**
     * 支付成功
     * @param array $postArr java返回的数据
     */
    public static function paySuccess($postArr, $order) {
        $resArr = $postArr;
        $order->pay_type = OrderPay::PAY_TYPE_BAIDUPAY;
        $order->status = ($resArr['pay_status'] == '1') ? Order::STATUS_PAY_END : Order::STATUS_PAY_FAIL;
        //$order->out_trade_no = $resArr['transaction_id']; //交易流水号
        $order->ptime = $resArr['pay_time'];
        $order->third_discount_fee += $resArr['ent_discount_fee']; //商家优惠
        $order->third_pay_discount_fee += $resArr['discount_fee']; //支付平台补贴
        $order->saveRedis();
        
        if($resArr['pay_status'] != '1') throw new \Exception($resArr['pay_msg']);
        
        $orderPay = new OrderPay();
        $orderPay->order_id = $order->id;
        $orderPay->enterprise_id = $order->enterprise_id;
        $orderPay->shop_id = $order->shop_id;
        $orderPay->befrom = $order->befrom;
        $orderPay->device_no = $order->device_no;
        $orderPay->cashier_id = $order->cashier_id;
        $orderPay->pay_type = OrderPay::PAY_TYPE_BAIDUPAY;
        $orderPay->pay_fee = $resArr['receipt_amount']; //商家实收金额
        $orderPay->payee = $resArr['account'];  //收款人
        $orderPay->payer = $resArr['payer'];    //付款人
        $orderPay->trade_id = $resArr['transaction_id'];//交易流水号
        $orderPay->utime = $resArr['pay_time'];
        $orderPay->discount_fee = $order->discount_fee;
        $orderPay->third_discount_fee = $order->third_discount_fee;
        $orderPay->saveRedis();
    }

    public function cancel(array $paras) {
        
    }

    public function checkNotifySign($paras) {
        
    }

    public function getSign($paras) {
        
    }

    public function orderquery(array $paras) {
        
    }

    public function prepay(array $paras) {
        
    }

    public function refund(array $paras) {
        
    }

    public function refundquery(array $paras) {
        
    }

}